package com.jinlia.show.modules.base.system.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.modules.base.constant.DeliveryStatusEnum;
import com.jinlia.show.modules.base.constant.EnableStatusEnum;
import com.jinlia.show.modules.base.constant.ErrandOrderStatusEnum;
import com.jinlia.show.modules.base.constant.i.IBaseEnum;
import com.jinlia.show.modules.base.system.entity.DeliveryOrder;
import com.jinlia.show.modules.base.system.entity.ErrandDadaCallback;
import com.jinlia.show.modules.base.system.entity.ErrandOrder;
import com.jinlia.show.modules.base.system.mapper.ErrandDadaCallbackMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;

@Slf4j
@Service
public class ErrandDadaCallbackMapperService extends ServiceImpl<ErrandDadaCallbackMapper, ErrandDadaCallback> {
    @Autowired
    private ErrandOrderMapperService errandDadaMapperService;
    @Autowired
    private DeliveryOrderMapperService deliveryOrderMapperService;

    /**
     * 回调接口
     *
     * @param paramMap
     */
    public void callbackListener(Long errandId, HashMap<String, Object> paramMap) {
        //达达跑腿回调日志
        ErrandDadaCallback deliveryDadaCallback = new ErrandDadaCallback();
        deliveryDadaCallback.setCallbackTime(LocalDateTime.now());
        deliveryDadaCallback.setErrandStatus(Integer.valueOf(paramMap.get("order_status").toString()));
        deliveryDadaCallback.setResponse(JSON.toJSONString(paramMap));
        deliveryDadaCallback.setErrandOrderId(errandId);
        this.save(deliveryDadaCallback);
        //分发回调
        this.dispatchHandler(errandId, paramMap);
    }

    /**
     * 订单状态(待接单＝1,待取货＝2,骑士到店=100,配送中＝3,已完成＝4,已取消＝5, 已追加待接单=8,妥投异常之物品返回中=9,
     * 妥投异常之物品返回完成=10, 售后取件单送达门店=6, 创建达达运单失败=1000）
     *
     * @param errandId
     * @param paramMap
     */
    private void dispatchHandler(Long errandId, HashMap<String, Object> paramMap) {
        ErrandOrder byIdErrandOrder = errandDadaMapperService.getById(errandId);
        //
        String orderStatus = paramMap.get("order_status").toString();
        LambdaUpdateChainWrapper<DeliveryOrder> deliveryOrderLambdaUpdateChainWrapper = deliveryOrderMapperService.lambdaUpdate()
                .eq(DeliveryOrder::getId, byIdErrandOrder.getDeliveryOrderId());
        LambdaUpdateChainWrapper<ErrandOrder> errandOrderLambdaUpdateChainWrapper = errandDadaMapperService.lambdaUpdate()
                .eq(ErrandOrder::getId, errandId);
        if ("1".equals(orderStatus)) {//待接单

        }
        if ("2".equals(orderStatus)) {//待取货
            //更新跑腿单状态
            errandOrderLambdaUpdateChainWrapper
                    .set(ErrandOrder::getTransporterName, paramMap.get("dm_mobile"))
                    .set(ErrandOrder::getTransporterPhone, paramMap.get("dm_name"));
//            deliveryOrderLambdaUpdateChainWrapper
//                    .set(DeliveryOrder::getDeliveryStatus, DeliveryStatusEnum.WAIT_ERRAND.getValue())
//                    .update();

        }
        if ("100".equals(orderStatus)) {//配送中
            //返现订单状态
//            deliveryOrderLambdaUpdateChainWrapper
//                    .set(DeliveryOrder::getDeliveryStatus, DeliveryStatusEnum.WAIT_ERRAND.getValue())
//                    .update();

        }
        if ("3".equals(orderStatus)) {//配送中
            //返现订单状态
//            deliveryOrderLambdaUpdateChainWrapper
//                    .set(DeliveryOrder::getDeliveryStatus, DeliveryStatusEnum.ON_WAY.getValue())
//                    .update();

        }
        if ("4".equals(orderStatus)) {//已完成
//            deliveryOrderLambdaUpdateChainWrapper
//                    .set(DeliveryOrder::getDeliveryStatus, DeliveryStatusEnum.ARRIVED.getValue())
//                    .update();

        }
        if ("5".equals(orderStatus)) {//已取消
//            deliveryOrderLambdaUpdateChainWrapper
//                    .set(DeliveryOrder::getDeliveryStatus, DeliveryStatusEnum.WAIT_ERRAND.getValue())
//                    .update();

        }
        //更新跑腿单状态
        errandOrderLambdaUpdateChainWrapper
                .set(ErrandOrder::getErrandOrderStatus, orderStatus)
                .update();
    }

}
